package com.xtianzhuang.www.controller.user.story;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.VUserStory;
import com.xtianzhuang.www.entity.VUserStoryExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.SaveStoryRequest;
import com.xtianzhuang.www.entity.request.StoryListQueryRequest;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.SaveStoryResponse;
import com.xtianzhuang.www.entity.response.StoryListQueryResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;

@Controller
@RequestMapping("/user")
public class StoryController extends BaseController {
	@RequestMapping(value = "/addStory")
	public ModelAndView addStory(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/story-add");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		return modelAndView;
	}

	@RequestMapping(value = "/storyList")
	public ModelAndView targetList(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/story-list");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		return modelAndView;
	}

	@RequestMapping(value = "/ajaxSaveStory")
	@ResponseBody
	@Log(type = LogType.USER)
	public SaveStoryResponse ajaxSaveStory(SaveStoryRequest request) {
		SaveStoryResponse response = new SaveStoryResponse();
		try {
			userServiceBiz.saveStory(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			logger.error("ajaxSaveStory error {}" + e.getMessage());
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/ajaxQueryStoryList")
	@ResponseBody
	@Log(type = LogType.USER)
	public StoryListQueryResponse queryStoryList(StoryListQueryRequest request) {
		StoryListQueryResponse response = new StoryListQueryResponse();
		VUserStoryExample example = new VUserStoryExample();
		VUserStoryExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(subject.getPrincipal().getUserId());
		if (StringUtils.isNotEmpty(request.getStoryType())) {
			criteria.andStoryTypeEqualTo(request.getStoryType());
		}
		long count = userServiceBiz.userStoryCountQuery(example);
		example.setOffset(request.getOffset());
		example.setLimit(request.getLimit());
		List<VUserStory> list = userServiceBiz.userStoryQuery(example);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("查询成功");
		response.setData(list);
		response.setTotal(count);
		return response;
	}
}
